Ext.namespace("TR.admin.trManage");

TR.admin.trManage.exercise = function(panelId, title, iconClz){

	var userSelectFlag = false;
	var exerciseStore = new Ext.data.JsonStore({
		url : "admin/exercise/findExerciseByCon",
		totalProperty : "total",
		root : "rows",
		fields : ["id", "name", "test", "testName", "status",
			"createDate", "updateDate", "authorName", "des"
		],
		autoLoad : true,
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					testId : testCombo.getValue(),
					statusStr : statusCombo.getValue(),
					con : queryField.getValue()
				});
			}
		}
	});
	var exerciesCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "心理测试",
		dataIndex : "name",
		width : 200
	}, {
		header : "测试类型",
		dataIndex : "testName"
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderEnumStatus
	}, {
		header : "创建者",
		dataIndex : "authorName"
	}, {
		header : "创建时间",
		dataIndex : "createDate",
		width : 130,
		renderer : renderDate
	}, {
		header : "更新时间",
		dataIndex : "updateDate",
		width : 130,
		renderer : renderDate
	}]);
	var exerciseSm = new Ext.grid.RowSelectionModel({
		singleSelect : true,
		listeners : {
			rowselect : function(){
				clazzStore.load()
			}
		}
	});
	var optBar = genOptBarByFn(create, del, update, toggle);
	var testCombo = genTestCombo();
	var statusCombo = genStatusCombo();
	var queryField = new Ext.form.TextField({});
	var queryBar = ["测试类型:", testCombo, "状态:", statusCombo, "关键词:", queryField, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			exerciseStore.load();
		}
	}];
	var exerciseGrid = new Ext.grid.GridPanel({
		cm : exerciesCm,
		sm : exerciseSm,
		flex : 8,
		frame : true,
		store : exerciseStore,
		tbar : genToolBar(optBar, queryBar, panelId)
	});

	var clazzStore = new Ext.data.JsonStore({
		url : "admin/exerciseClazz/listClazzByExercise",
		root : "rows",
		fields : ["id", "name", "status", "enterSchoolDate", "graduateDate",
			"createDate", "updateDate", "des", "authorName", "owner"
		],
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					exerciseId : exerciseSm.getSelected().get("id")
				});
			}
		}
	});
	var clazzSm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	var clazzCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "班级名称",
		dataIndex : "name"
	}, {
		header : "入校时间",
		dataIndex : "enterSchoolDate",
		renderer : renderDate
	}]);
	var clazzBar = ["-", {
		text : "更新",
		iconCls : "update",
		handler : browseClazz
	}, "-"];
	var clazzGrid = new Ext.grid.GridPanel({
		title : "班级",
		cm : clazzCm,
		sm : clazzSm,
		store : clazzStore,
		flex : 3,
		frame : true,
		tbar : clazzBar
	});

	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "hbox",
		layoutConfig : {
			align : "stretch"
		},
		items : [exerciseGrid, clazzGrid]
	});

	function create(){
		var win = genAddOrUpdateWin("admin/exercise/add");
		win.show();
	}
	function del(){
		var rec =exerciseSm.getSelected();
		if(rec==null){
			showFailMsg("请选择心理测试");
			return;
		}
		confirmDelete(remove);
		function remove(){
			ajaxRequest({
				url : "admin/exercise/delete/" + rec.get("id"),
				callback : function(){
					exerciseStore.load();
				}
			});
		}
	}
	function update(){
		var rec =exerciseSm.getSelected();
		if(rec==undefined){
			showFailMsg("请选择心理测试");
			return;
		}
		var win = genAddOrUpdateWin("admin/exercise/update");
		win.findByType("form")[0].getForm().loadRecord(rec);
		win.show();
	}
	function toggle(){
		var rec =exerciseSm.getSelected();
		if(rec==null){
			showFailMsg("请选择心理测试");
			return;
		}
		ajaxRequest({
			url : "admin/exercise/toggleStatus/" + rec.get("id"),
			callback : function(){
				exerciseStore.reload();
			}
		});
	}

	function genAddOrUpdateWin(url){
		return new Ext.Window({
			title : "新增或修改心理测试",
			layout : "fit",
			modal : true,
			iconCls : "save",
			width : 320,
			height : 200,
			plain : true,
			bodyStyle : 'padding:5px;',
			items : genForm(url)
		});
		function genForm(url){
			return new Ext.form.FormPanel({
				url : url,
				labelWidth : 80,
				labelAlign : "right",
				baseCls: 'x-plain',
				defaults : {
					xtype : "textfield",
					anchor : "95%"
				},
				items : [{
					fieldLabel:"名称",
					name : "name",
					allowBlank : false
				}, genTestCombo(), genStatusCombo(), {
					fieldLabel : "描述",
					name : "des"
				}],
				buttonAlign : "center",
				buttons : [{
					text : "保存",
					iconCls : "save",
					handler : function(b){
						submitForm(b.findParentByType("form"), refreshGrid);
						function refreshGrid(){
							exerciseStore.reload();
						}
					}
				}, {
					text : "重置",
					iconCls : "reset",
					type : "reset",
					handler : function(b){
						b.findParentByType("form").getForm().reset();
					}
				}]
			});
		}
	}

	function browseClazz(){
		var rec = exerciseSm.getSelected();
		if(rec==null){
			showFailMsg("请选择心理测试");
			return;
		}
		genBrowseClazzWin().show();
	}
	function genBrowseClazzWin(){
		var cStore = new Ext.data.JsonStore({
			autoLoad : true,
			url : "admin/clazz/listInSchoolClazz",
			root : "rows",
			fields : ["id", "name", "status", "enterSchoolDate", "graduateDate",
				"createDate", "updateDate", "des", "authorName", "owner"
			],
			listeners : {
				load : init
			}
		});
		var cSm = new Ext.grid.CheckboxSelectionModel({
			checkOnly : true,
			listeners : {
				rowselect : addClazzToExercise,
				rowdeselect : delClazzFromExercise
			}
		});
		var cCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), cSm, {
			header : "班级名称",
			dataIndex : "name"
		}, {
			header : "入校时间",
			dataIndex : "enterSchoolDate",
			renderer : renderDate
		}]);
		var cGrid = new Ext.grid.GridPanel({
			sm : cSm,
			cm : cCm,
			store : cStore
		});
		return new Ext.Window({
			title : "选择班级",
			layout : "fit",
			modal : true,
			iconCls : "save",
			width : 290,
			height : 400,
			plain : true,
			bodyStyle : 'padding:5px;',
			items : cGrid
		});
		function init(s, allClazz){
			userSelectFlag = false;
			var range = [];
			clazzStore.each(function(v){
				for(var i=0; i<allClazz.length; i++){
					var rec = allClazz[i];
					if(rec.get("id")===v.get("id")){
						range.push(i);
						break;
					}
				}
			});
			cSm.selectRows(range);
			userSelectFlag = true;
		}
		function addClazzToExercise(sm, rowIndex, clazzRec){
			if(userSelectFlag===false){
				return;
			}
			var exerciseRec = exerciseSm.getSelected();
			ajaxRequest({
				url : "admin/exerciseClazz/add",
				params : {
					exerciseId : exerciseRec.get("id"),
					clazzId : clazzRec.get("id"),
					status : exerciseRec.get("status")
				},
				callback : function(){
					clazzStore.reload();
				}
			});
		}
		function delClazzFromExercise(sm, rowIndex, clazzRec){
			if(userSelectFlag===false){
				return;
			}
			var exerciseRec = exerciseSm.getSelected();
			ajaxRequest({
				url : "admin/exerciseClazz/delete",
				params : {
					exerciseId : exerciseRec.get("id"),
					clazzId : clazzRec.get("id")
				},
				callback : function(){
					clazzStore.reload();
				}
			});
		}
	}
}

registerModule("TR.admin.trManage.exercise", TR.admin.trManage.exercise);
